﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Data;

using Csla;
using lite;
using Csla.Core;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using OpenExpressApp;
namespace ZX.BPM.Library
{
    public  class WorkTaskFormRoles
    {
        /// <summary>
        /// 流程模板ID
        /// </summary>
        public string WorkFlowId {get;set;}
        /// <summary>
        /// 流程模板任务ID
        /// </summary>
        public string WorkTaskId { get; set; }
        /// <summary>
        /// 表单编号
        /// </summary>
        public string FormID { get; set; }

        /// <summary>
        /// 主表表单权限信息
        /// </summary>
        public FormTableRole MasterPropertyValue { get; set; }
        /// <summary>
        /// 明细表表单权限信息
        /// </summary>
        public Hashtable DetailsPropertyValue { get; set; }


        public WorkTaskFormRoles(string xWorkFlowId, string xWorkTaskId,string xFormID) 
        {
            WorkFlowId = xWorkFlowId;
            WorkTaskId = xWorkTaskId;
            FormID = xFormID;

            IGTable dt = null;
            using (var db = DBHelper.CreateDb(ZX.DAL.ConnectionStringNames.OpenExpressApp))
            {
                string strSql=" select * from HF_WorkTaskFormValue where WorkFlowId='"+xWorkFlowId+"' and WorkTaskId='"+xWorkTaskId+"'";
                string strSql1 = " select [WorkFlowId] ,[WorkTaskId],[FormID],[TableType],[TableName] from HF_WorkTaskFormValue where WorkFlowId='" + xWorkFlowId + "' and WorkTaskId='" + xWorkTaskId + "'";
                dt = db.QueryTable(strSql);
                DataTable formTable = dt.GetTable;
                DataTable typeTables = db.QueryTable(strSql1).GetTable;
                MasterPropertyValue = getMasterPropertyValue(formTable, typeTables);
                DetailsPropertyValue = getDetailsItems(formTable, typeTables);
            }

       
        }
        //private string CreateWorkTaskFormRoles(string xWorkFlowId, string xWorkTaskId, string xFormID) 
        //{
        //    string _formRoles = "";
        //    WorkTaskFormRoles fr = new WorkTaskFormRoles(xWorkFlowId, xWorkTaskId, xFormID);
        //    string _Master_Readonly= " var Master_Readonly=" + ZX.Common.Globals.ObjectToJSON(fr.MasterPropertyValue.ReadonlyPropertyValue);
        //    string _Master_Required = "\r\n  var Master_Readonly=" + ZX.Common.Globals.ObjectToJSON(fr.MasterPropertyValue.RequiredPropertyValue);
        //    string _Master_Hide = "\r\n  var Master_Readonly=" + ZX.Common.Globals.ObjectToJSON(fr.MasterPropertyValue.HidePropertyValue);
        //    string _Master_Editor = "\r\n  var Master_Readonly=" + ZX.Common.Globals.ObjectToJSON(fr.MasterPropertyValue.EditorPropertyValue);
        //    return _formRoles = _Master_Readonly + _Master_Required + _Master_Hide;
        //}

        private FormTableRole getMasterPropertyValue(DataTable xTable, DataTable typeTables)
        {
            FormTableRole _MasterTable=null;

            //DataTable dt = getFormTables(xTable);
            DataRow[] row = getTableName(typeTables, "Master");

            string masterTableName = "";
            if (row.Length > 0)
            {
                masterTableName = row[0]["TableName"].ToString();
            }
            if (masterTableName != "")
            {
                _MasterTable = new FormTableRole(xTable, masterTableName);
            }
            return _MasterTable;
        }
        private Hashtable getDetailsItems(DataTable xTable, DataTable typeTables)
        {
            Hashtable htItems = new Hashtable();
           // DataTable dt = getFormTables(xTable);
            DataRow[] row = getTableName(typeTables, "Details");
            //if (row.Length > 0)
            //{
            //    foreach (DataRow rw in row)
            //    {
            //        string _talbeName = rw["TableName"].ToString();
            //        if (!string.IsNullOrEmpty(_talbeName))
            //        {
            //            FormTableRole ft = new FormTableRole(xTable, _talbeName);
            //            if (ft != null)
            //            {
            //                string strJson = ZX.Common.Globals.ObjectToJSON(ft);
            //                htItems.Add(_talbeName, strJson);
            //            }
            //        }
            //    }
            //}
            string _talbeName = "";
            if (row.Length > 0)
            {
                _talbeName = row[0]["TableName"].ToString();
            }
            if (_talbeName != "")
            {
                FormTableRole ft = new FormTableRole(xTable, _talbeName);
                if (ft != null)
                {
                  
                    string strJson = ZX.Common.Globals.ObjectToJSON(ft);
                    htItems.Add(_talbeName, strJson);
                }
            }
            return htItems;
        }
        /// <summary>
        /// 获取表名
        /// </summary>
        /// <param name="xTable"></param>
        /// <param name="xTableType"></param>
        /// <returns></returns>
        private DataRow[] getTableName(DataTable xTable, string xTableType) 
        {

            DataRow[] row = xTable.Select("TableType='" + xTableType + "'");
            return row;
        }
        private DataTable  getFormTables(DataTable xTable)
       {
           DataTable dt = new DataTable();
           dt = xTable;
           dt.Columns.Remove("TableField");
           dt.Columns.Remove("State");
           dt.Columns.Remove("IsRequired");
           dt.Columns.Remove("AddTime");
           dt.Columns.Remove("AddUser");
           dt.Columns.Remove("ID");


            string[] distinctcols = new string[(xTable.Columns.Count)];
            foreach (DataColumn dc in dt.Columns)
            {
                distinctcols[dc.Ordinal] = dc.ColumnName;
            }

            DataTable podt = new DataTable("personFilterDup");
            DataView xdataview = new DataView(dt);
            podt = xdataview.ToTable(true, distinctcols);
            return podt;
       }
    }

    public class FormTableRole
    {
        public string TableName { get; set; }
        public ArrayList ReadonlyPropertyValue { get; set; }
        public ArrayList EditorPropertyValue { get; set; }
        public ArrayList HidePropertyValue { get; set; }
        public ArrayList RequiredPropertyValue { get; set; }

        public FormTableRole(DataTable xTable,string xTableName) 
        { 
            TableName = xTableName;
            ReadonlyPropertyValue = getPropertyValue(xTable, "readonly");
            EditorPropertyValue = getPropertyValue(xTable, "editor");
            HidePropertyValue = getPropertyValue(xTable, "hide");
            RequiredPropertyValue = getRequiredPropertyValue(xTable, "editor");
        }
        private ArrayList getPropertyValue(DataTable xTable, string xState) 
        {
            ArrayList values=new ArrayList();
            IEnumerable<DataRow> query = from row in xTable.AsEnumerable()
                                         where row.Field<string>("TableName") == TableName &&
                                               row.Field<string>("State") == xState
                                         select row;
            foreach (DataRow r in query)
            {
                values.Add(r["TableField"].ToString());
            }
            return values;
        }
        private ArrayList getRequiredPropertyValue(DataTable xTable, string xState)
        {
            ArrayList values = new ArrayList();
            IEnumerable<DataRow> query = from row in xTable.AsEnumerable()
                                         where row.Field<string>("TableName") == TableName &&
                                               row.Field<string>("State") == xState &&
                                               row.Field<string>("IsRequired") == "Y"
                                         select row;
            foreach (DataRow r in query)
            {
                values.Add(r["TableField"].ToString());
            }
            return values;
        }
    }

}
